//
//  NetworkTools.swift
//  WBDemo
//
//  Created by Jin on 23/04/2018.
//  Copyright © 2018 com.baidu.123. All rights reserved.
//

import UIKit
import AFNetworking

enum RequestType {
    case GET
    case POST
}


class NetworkTools: AFHTTPSessionManager {
    
    //单例
    static let shareInstance:NetworkTools = {
        let url = URL(string: "https://api.weibo.com/")
        let instance = NetworkTools(baseURL: url, sessionConfiguration: URLSessionConfiguration.default)
        instance.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return instance
    }()
    
}

//封装请求方法
extension NetworkTools {
    private func request(methodType:RequestType,URLString:String,parameters:[String:AnyObject],finished:@escaping (_ result:Any?,_ error:Error?)->Void){
        
        //定义成功和失败回调闭包
        let successCallBack = { (task : URLSessionDataTask?, objc : Any?) in
            finished(objc,nil)
        }
        let failureCallBack = { (task : URLSessionDataTask?, error : Error?) in
            finished(nil,error)
        }
        
        
        if methodType == .GET {
            get(URLString, parameters: parameters, progress: nil, success: successCallBack,failure:failureCallBack)
        }else{
            post(URLString, parameters: parameters, progress: nil, success: successCallBack,failure:failureCallBack)
        }
    }
}

//请求AccessToken
extension NetworkTools {
    func loadUserAccessToken(codeStr:String,finished:@escaping (_ result:Any?,_ error:Error?)->Void) {
        let path = "oauth2/access_token"
        let parameters =
            [
                "client_id":WB_App_Key,
                "client_secret":WB_App_Secret,
                "redirect_uri":WB_Redirect_URI,
                "grant_type":"authorization_code",
                "code":codeStr
        ]
        
        request(methodType: .POST, URLString: path, parameters: parameters as [String : AnyObject]) { (result, error) in
            finished(result,error)
        }
    }
}

//获取用户信息
extension NetworkTools {
    func loadUserInfo(access_token:String,uid:String,finished:@escaping (_ result:Any?,_ error:Error?)->Void) {
        let path = "2/users/show.json"
        let parameters = ["access_token":access_token,
                          "uid":uid]
        
        request(methodType: .GET, URLString: path, parameters: parameters as [String : AnyObject]) { (result, error) in
            finished(result,error)
        }
    }
}

//获取首页信息
extension NetworkTools {
    func loadHomePageInfo(since_id:String,max_id:String,access_token:String,finished:@escaping (_ result:Any?,_ error:Error?)->Void) {
        let path = "2/statuses/home_timeline.json"
        let parameters = ["access_token":access_token,"since_id":since_id,"max_id":max_id]
        
        request(methodType: .GET, URLString: path, parameters: parameters as [String : AnyObject]) { (result, error) in
            
            finished(result,error)
        }
    }
}

//发送微博
extension NetworkTools {
    func sendMessageStatus(statusText:String,access_token:String,isSuccess:@escaping (_ isSuccess:Bool)->Void) {
        let path = "2/statuses/update.json"
        let parameters = ["statusText":statusText,"access_token":access_token]
        
        request(methodType: .POST, URLString: path, parameters: parameters as [String : AnyObject]) { (result, error) in
            if result != nil {
                isSuccess(true)
            }else{
                isSuccess(false)
            }
        }
    }
}
